﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.Framework.Server.Base;
using ZhimeiSoft.CharmManager.Server.DAL.Interface;


namespace ZhimeiSoft.CharmManager.Server.DAL
{
    public class EmployeeDAL : BaseDal, IEmployeeDAL
    {
        /// <summary>
        /// 设置SQL语句中的参数
        /// </summary>
        /// <param name="cmd">DbCommand对象</param>
        /// <param name="employee">员工实体类</param>
        private void AddInSqlParameters(ref DbCommand cmd, DBEntityEmployee employee)
        {
            if (employee == null || cmd == null)
                return;
            
            DB.AddInParameter(cmd, DBEntityEmployee.FieldName.ENAME, DbType.String, employee.ENAME);
            DB.AddInParameter(cmd, DBEntityEmployee.FieldName.EMPLOYEENAME, DbType.String, employee.EMPLOYEENAME);
            DB.AddInParameter(cmd, DBEntityEmployee.FieldName.EADDRESS, DbType.String, employee.EADDRESS);
            DB.AddInParameter(cmd, DBEntityEmployee.FieldName.EBIRTHDAY, DbType.DateTime, employee.EBIRTHDAY);
            DB.AddInParameter(cmd, DBEntityEmployee.FieldName.ECARD, DbType.String, employee.ECARD);
            DB.AddInParameter(cmd, DBEntityEmployee.FieldName.ECODE, DbType.String, employee.ECODE);
            DB.AddInParameter(cmd, DBEntityEmployee.FieldName.EDUCATION, DbType.String, employee.EDUCATION);
            DB.AddInParameter(cmd, DBEntityEmployee.FieldName.ENTRYDAY, DbType.DateTime, employee.ENTRYDAY);
            DB.AddInParameter(cmd, DBEntityEmployee.FieldName.EFAMLIYPHONE, DbType.String, employee.EFAMLIYPHONE);
            DB.AddInParameter(cmd, DBEntityEmployee.FieldName.EINDEXCODE, DbType.String, employee.EINDEXCODE);
            DB.AddInParameter(cmd, DBEntityEmployee.FieldName.EMOBILEPHONE, DbType.String, employee.EMOBILEPHONE);
            DB.AddInParameter(cmd, DBEntityEmployee.FieldName.EPASSWORD, DbType.String, employee.EPASSWORD);
            DB.AddInParameter(cmd, DBEntityEmployee.FieldName.EQUITDAY, DbType.DateTime, employee.EQUITDAY);
            DB.AddInParameter(cmd, DBEntityEmployee.FieldName.SALONID, DbType.Int32, employee.SALONID);
            DB.AddInParameter(cmd, DBEntityEmployee.FieldName.ESTATE, DbType.String, employee.ESTATE);
            DB.AddInParameter(cmd, DBEntityEmployee.FieldName.ISRESERVERATION, DbType.Int16, employee.ISRESERVERATION);
        }

        /// <summary>
        /// 新增员工信息
        /// </summary>
        /// <param name="employee">员工信息实体类</param>
        /// <returns>员工ID</returns>
        public void InsertEmployee(DBEntityEmployee employee)
        {
            string strSQL = EmployeeDAL_Const.SQL_INSERT_EMPLOYEE;
            DbCommand cmdSelect = DB.GetSqlStringCommand(strSQL);
            AddInSqlParameters(ref cmdSelect, employee);
            DB.ExecuteNonQuery(cmdSelect);
        }

        /// <summary>
        /// 更新员工信息
        /// </summary>
        /// <param name="employee">员工信息实体类</param>
        public void UpdateEmployee(DBEntityEmployee employee)
        {
            string strSQL = EmployeeDAL_Const.SQL_UPDATE_EMPLOYEE;
            DbCommand cmdUpdate = DB.GetSqlStringCommand(strSQL);
            AddInSqlParameters(ref cmdUpdate, employee);
            DB.AddInParameter(cmdUpdate, "EMPLOYEEID", DbType.Int32, employee.EMPLOYEEID);
            DB.ExecuteNonQuery(cmdUpdate);
        }

        /// <summary>
        /// 删除员工信息
        /// </summary>
        /// <param name="employeeid">员工ID</param>
        public void DeleteEmployee(int employeeid)
        {
            string strSQL = EmployeeDAL_Const.SQL_DELETE_EMPLOYEE;
            DbCommand cmdDelete = DB.GetSqlStringCommand(strSQL);
            DB.AddInParameter(cmdDelete, DBEntityEmployee.FieldName.EMPLOYEEID, DbType.Int32, employeeid);
            DB.ExecuteNonQuery(cmdDelete);
        }
        /// <summary>
        /// 根据状态查询员工记录
        /// </summary>
        /// <param name="state">状态</param>
        /// <returns>返回数据</returns>
        public DataSet QueryEmployeeState(string state)
        {
            DbCommand queryComm;
            if (state.Equals(string.Empty))
            {
                queryComm = DB.GetSqlStringCommand(EmployeeDAL_Const.SQL_QUERY_EMPLOYEE_ALL);
            }
            else
            {
                queryComm = DB.GetSqlStringCommand(EmployeeDAL_Const.SQL_QUERY_EMPLOYEE_STATE);
                DB.AddInParameter(queryComm, DBEntityEmployee.FieldName.ESTATE, DbType.String, state);
            }
            return DB.ExecuteDataSet(queryComm);
        }
        /// <summary>
        /// 根据条件过滤员工信息
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public DataSet QeuryEmployeeFilter(string filter) { 
            string sql = EmployeeDAL_Const.SQL_QUERY_EMPLOYEE + ( filter == string.Empty ? "" : " WHERE ") + filter;
            DbCommand queryComm = DB.GetSqlStringCommand(sql);
            return DB.ExecuteDataSet(queryComm);

        }

    }
}
